<?php

namespace App\Http\Model;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Comm\CommonController as common;


class Role extends Model
{

	/**
	 * [showListModel 添加时上级名称显示model]
	 * @Author   孙丽男
	 * @DateTime 2018-06-05
	 * @version  6.2
	 * @return   [json]    [$result]
	 */
	public function showParentModel(){

		$roleObj = Db::table('roles')
						->select(['id','name'])
						->get();
		$roleArr = json_decode(json_encode($roleObj),true);

		if ($roleArr) {
			
			$comm = new Common;
			//处理数组
			$data = $comm->createTree($roleArr);
			$result['code'] = 1000;
    		$result['msg'] = '查询成功';
    		$result['data'] = $data;
    		return $result;
		}else{

			$result['code'] = 1004;
    		$result['msg'] = '查询部门失败';
    		return $result;
		}

		return json_encode($result);
		
	}
	/**
	 * [addRoleModel 添加角色model]
	 * @Author   孙丽男
	 * @DateTime 2018-06-05
	 * @param    [角色信息]  array  $roleArr
	 * @version  6.2
	 */
	public function addRoleModel($roleArr){

		$result = array();
		$roleArr['update_user'] = session('username');
		$roleArr['update_time'] = date('Y-m-d H:i:s');

		//添加之前，查询该上级下有没有该角色名称
		$ro = Db::table('roles')
				  ->where(['pid'=>$roleArr['pid'], 'name'=> $roleArr['name']])
				  ->first();

		if ($ro) {
			
			$result['code'] = 1005;
			$result['msg'] = '父级已有该名称';
			return json_encode($result);
		}
		$role = Db::table('roles')
					->insert($roleArr);
		if ($role) {
				
			$result['code'] = 1000;
			$result['msg'] = '添加成功';

		}else{

			$result['code'] = 1004;
			$result['msg'] = '添加失败';
		}

		return json_encode($result);
	}

	/**
	 * [findUserModel 角色列表model]
	 * @Author   孙丽男
	 * @DateTime 2018-06-05
	 * @param    [查询条件]  array  $where
	 * @version  6.2
	 * @return   [json]     [$result]
	 */
	public function findRoleModel($where){

		$result = array();
		$rolesObj = Db::table('roles')
						->where($where)
						->orderBy('update_time', 'desc')
						->get();
		$rolesArr = json_decode(json_encode($rolesObj),true);

		if ($rolesArr) {

			//整理数组
			foreach ($rolesArr as $key => $value) {
				
				if ($value['pid'] != 0) {

					$pname = Db::table('roles')
						->where('id',$rolesArr[$key]['pid'])
						->select('name')
						->first();

					$pnameArr = json_decode(json_encode($pname), true);

					$rolesArr[$key]['pname'] = $pnameArr['name'];
					
				}else{

					$rolesArr[$key]['pname'] = '';
				}
			}
			$result['code'] = 1000;
			$result['msg'] = '查询成功';
			$result['data'] = $rolesArr;		

		}else{
			$result['code'] = 1004;
    		$result['msg'] = '查询失败';
		}
		return json_encode($result);
	}

	/**
	 * [deleteRoleModel 删除角色model]
	 * @Author   孙丽男
	 * @DateTime 2018-06-05
	 * @param    [角色id数组] array $roleIdArr
	 * @version  6.2
	 * @return   [json]     [$result]
	 */
	public function deleteRoleModel($roleIdArr){

		$comm = new Common;

		$admin = $comm->isAdmin(0,$roleIdArr);

		if ($admin) {

			$result['code'] = 1007;
	    	$result['msg'] = '不能删除超级管理员角色';
			return json_encode($result);

		}

		//删除用户
		$deleteRole = Db::table('roles')
						->whereIn('id', $roleIdArr)
						->delete();
		if ($deleteRole) {
			//删除用户角色
			$findUserRole = Db::table('user_role')
							->whereIn('role_id', $roleIdArr)
							->first();	
			if ($findUserRole) {
				$deleteUserRole = Db::table('user_role')
							->whereIn('role_id', $roleIdArr)
							->delete();
			}
			$result['code'] = 1000;
	    	$result['msg'] = '删除成功';
		}else{

			$result['code'] = 1004;
	    	$result['msg'] = '删除角色失败';
		}
		
		return json_encode($result);
	}

	/**
	 * [updateRoleModel 修改角色信息model]
	 * @Author   孙丽男
	 * @DateTime 2018-06-05
	 * @param    [角色id] int $roleId
	 * @param    [需要修改的信息] array $updateInfo
	 * @version  6.2
	 * @return   [json]     [$result]
	 */
	public function updateRoleModel($roleId, $updateInfo){

		$roleIdArr[] = $roleId;
		$comm = new Common;

		$admin = $comm->isAdmin(0,$roleIdArr);

		if ($admin) {

			$result['code'] = 1007;
	    	$result['msg'] = '不能修改超级管理员角色信息';
			return json_encode($result);

		}

		//查询该上级下有没有该角色名称
		$ro = Db::table('roles')
				  ->where(['pid'=>$updateInfo['pid'], 'name'=> $updateInfo['name']])
				  ->first();

		if ($ro) {
			
			$result['code'] = 1005;
			$result['msg'] = '父级已有该名称';
			return json_encode($result);
		}
		$updateInfo['update_user'] = session('username');
		$updateInfo['update_time'] = date('Y-m-d H:i:s');
		$updateRole = Db::table('roles')
						->where('id', $roleId)
						->update($updateInfo);
		if ($updateRole) {

			$result['code'] = 1000;
			$result['msg'] = '修改成功';

		}else{

			$result['code'] = 1004;
	    	$result['msg'] = '修改失败';
		}
		
		return json_encode($result);
	}

	/**
	 * [powerListModel 权限树状列表]
	 * @Author   孙丽男
	 * @DateTime 2018-06-05
	 * @version  6.2
	 * @return   [json]     [$result]
	 */
	public function powerListModel(){

		$powerObj = Db::table('power')
						->select(['id','name as value','pid'])
						->orderBy('sort')
						->get();
		$powerArr = json_decode(json_encode($powerObj),true);

		if ($powerArr) {
			
			$comm = new Common;
			//处理数组
			$data = $comm->createTree($powerArr);
			$result['code'] = 1000;
    		$result['msg'] = '查询成功';
    		$result['data'] = $data;
    		return $result;
		}else{

			$result['code'] = 1004;
    		$result['msg'] = '没有权限数据';
    		return $result;
		}

		return json_encode($result);
	}

	/**
	 * [setRolesModel 为角色设置权限]
	 * @Author   孙丽男
	 * @DateTime 2018-06-05
	 * @param    [角色id数组] array $roleIdArr
	 * @param    [权限id数组] array $powerIdArr
	 * @version  6.2
	 * @return   [json]     [$result]
	 */
	public function setPowerModel($roleIdArr , $powerIdArr){

		$comm = new Common;

		$admin = $comm->isAdmin(0,$roleIdArr);

		if ($admin) {

			$result['code'] = 1007;
	    	$result['msg'] = '不能修改超级管理员权限';
			return json_encode($result);

		}

		$result = array();

		//1、清空角色的权限 2、添加角色与权限
		$delete = Db::table('role_power')
					->whereIn('role_id', $roleIdArr)
					->delete();
		//构建添加的数据 
		$i = 0;		//计数器
		foreach ($roleIdArr as $key => $value) {
			
			foreach ($powerIdArr as $k => $v) {
				
				$data[$i]['role_id'] = $value;
				$data[$i]['power_id'] = $v;
				$i++;
			}

		}
		$add = Db::table('role_power')
				->insert($data);

		if ($add) {
			
			$result['code'] = 1000;
            $result['msg'] = '分配权限成功';

		}else{

			$result['code'] = 1004;
            $result['msg'] = '分配权限失败';
		}

		return json_encode($result);
	}

}